package cn.csc.module.shop.dal.mysql.detail;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.shop.controller.admin.detail.vo.ShopDetailExportReqVO;
import cn.csc.module.shop.controller.admin.detail.vo.ShopDetailPageReqVO;
import cn.csc.module.shop.controller.admin.detail.vo.ShopDetailWithAuditTimeRespVO;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailRespVO;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailUpdateShopLogoReqVO;
import cn.csc.module.shop.dal.dataobject.detail.ShopDetailDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigInteger;
import java.util.List;

/**
 * 店铺详情 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface ShopDetailMapper extends BaseMapperX<ShopDetailDO> {

    default PageResult<ShopDetailDO> selectPage(ShopDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ShopDetailDO>()
                .eqIfPresent(ShopDetailDO::getAppUserId, reqVO.getAppUserId())
                .likeIfPresent(ShopDetailDO::getShopName, reqVO.getShopName())
                .eqIfPresent(ShopDetailDO::getShopStatus, reqVO.getShopStatus())
                .eqIfPresent(ShopDetailDO::getBusinessType, reqVO.getBusinessType())
                .eqIfPresent(ShopDetailDO::getBusinessId, reqVO.getBusinessId())
                .likeIfPresent(ShopDetailDO::getBusinessName, reqVO.getBusinessName())
                .likeIfPresent(ShopDetailDO::getLegalPersonName, reqVO.getLegalPersonName())
                .eqIfPresent(ShopDetailDO::getContact, reqVO.getContact())
                .eqIfPresent(ShopDetailDO::getPhoneNumber, reqVO.getPhoneNumber())
                .eqIfPresent(ShopDetailDO::getIdCardNo, reqVO.getIdCardNo())
                .eqIfPresent(ShopDetailDO::getType, reqVO.getType())
                .betweenIfPresent(ShopDetailDO::getCreateTime, reqVO.getCreateTime())
                .eq(ShopDetailDO::getDeleted, 0)
                .orderByDesc(ShopDetailDO::getId));
    }


    Page<ShopDetailWithAuditTimeRespVO> selectPageWithAuditTime(@Param("req") ShopDetailPageReqVO reqVO, IPage<ShopDetailWithAuditTimeRespVO> page);


    default List<ShopDetailDO> selectList(ShopDetailExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ShopDetailDO>()
                .eqIfPresent(ShopDetailDO::getAppUserId, reqVO.getAppUserId())
                .likeIfPresent(ShopDetailDO::getShopName, reqVO.getShopName())
                .eqIfPresent(ShopDetailDO::getIntro, reqVO.getIntro())
                .eqIfPresent(ShopDetailDO::getShopLogo, reqVO.getShopLogo())
                .eqIfPresent(ShopDetailDO::getMobileBackgroundPic, reqVO.getMobileBackgroundPic())
                .eqIfPresent(ShopDetailDO::getShopStatus, reqVO.getShopStatus())
                .eqIfPresent(ShopDetailDO::getBusinessType, reqVO.getBusinessType())
                .eqIfPresent(ShopDetailDO::getBusinessId, reqVO.getBusinessId())
                .likeIfPresent(ShopDetailDO::getBusinessName, reqVO.getBusinessName())
                .likeIfPresent(ShopDetailDO::getLegalPersonName, reqVO.getLegalPersonName())
                .eqIfPresent(ShopDetailDO::getContact, reqVO.getContact())
                .eqIfPresent(ShopDetailDO::getPhoneNumber, reqVO.getPhoneNumber())
                .eqIfPresent(ShopDetailDO::getAddress, reqVO.getAddress())
                .eqIfPresent(ShopDetailDO::getBusinessLicense, reqVO.getBusinessLicense())
                .eqIfPresent(ShopDetailDO::getIdCardNo, reqVO.getIdCardNo())
                .eqIfPresent(ShopDetailDO::getIdCardFront, reqVO.getIdCardFront())
                .eqIfPresent(ShopDetailDO::getIdCardLater, reqVO.getIdCardLater())
                .eqIfPresent(ShopDetailDO::getType, reqVO.getType())
                .betweenIfPresent(ShopDetailDO::getCreateTime, reqVO.getCreateTime())
                .eq(ShopDetailDO::getDeleted, 0)
                .orderByDesc(ShopDetailDO::getId));
    }

    @Select("SELECT d.id,d.app_user_id, d.shop_name, d.company_name, d.intro, d.shop_logo, d.shop_status, d.business_type, d.business_id, " +
            " d.business_name, d.legal_person_name, d.contact, d.phone_number, d.address, d.business_license, d.certificate_type, " +
            " d.id_card_no, d.audit_status, d.type, d.create_time, d.update_time, d.deleted" +
            " FROM shop_detail d INNER JOIN shop_user u ON d.id = u.shop_id WHERE u.user_id = #{userId}")
    ShopDetailDO selectDetailByUserId(@Param("userId") BigInteger userId);


    default ShopDetailDO selectSimpleDetailById(BigInteger shopId){
        return selectOne(new LambdaQueryWrapperX<ShopDetailDO>()
                // 查询指定字段
                .select(ShopDetailDO::getShopName,ShopDetailDO::getBusinessLicense,ShopDetailDO::getAppUserId)
                .eq(ShopDetailDO::getId,shopId));
    }

    default ShopDetailDO selectByIdCardNo(String idCardNo){
        return selectOne(new LambdaQueryWrapperX<ShopDetailDO>()
                // 查询指定字段
                .select(ShopDetailDO::getIdCardNo,ShopDetailDO::getId)
                .eq(ShopDetailDO::getIdCardNo,idCardNo));
    }

    Page<AppShopDetailRespVO> selectSimpleDetailPage(@Param("req") ShopDetailPageReqVO reqVO, IPage<AppShopDetailRespVO> page);


    List<ShopDetailDO> selectShopDetailList(List<BigInteger> shopIds);

    /**
     * 更新ShopLogo
     * 更新部分字段
     * @param reqVO 请求参数
     * @return
     */
    default Integer updateShopLogo(AppShopDetailUpdateShopLogoReqVO reqVO){
        LambdaUpdateWrapper<ShopDetailDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(ShopDetailDO::getShopLogo, reqVO.getShopLogo());
        userUpdateWrapper.eq(ShopDetailDO::getId, reqVO.getId());
        return update(null, userUpdateWrapper);
    }


    /**
     * 更新审核状态
     * @param shopId 店铺ID
     * @param auditStatus 审核状态(0:不需审核 1:等待审核 2:审核通过 3:需补充资料 4:审核不通过)
     * @param auditType 店铺审核类型,1自营店 2普通店 3华商贸店铺 5采购商 10供货商 15采购商+供货商 20 选品卖家 30 供货商+卖家
     * @return
     */
    default Integer updateAuditStatus(BigInteger shopId, Integer auditStatus, Integer auditType){
        LambdaUpdateWrapper<ShopDetailDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(ShopDetailDO::getAuditStatus, auditStatus);
        if (auditStatus == 2){
            userUpdateWrapper.set(ShopDetailDO::getType, auditType);
            userUpdateWrapper.set(ShopDetailDO::getAuditType, 0);
        }
        userUpdateWrapper.eq(ShopDetailDO::getId, shopId);
        return update(null, userUpdateWrapper);
    }


}
